import { isObject } from 'lodash'
import { Catch } from '@nestjs/common'
import { BaseWsExceptionFilter, WsException } from '@nestjs/websockets'

@Catch(WsException)
export class WsExceptionFilter extends BaseWsExceptionFilter {
  handleError<TClient extends { emit: Function }>(client: TClient, exception: WsException): void {
    const action = 'error'
    const result = exception.getError()
    const message = isObject(result)
      ? result
      : {
          action,
          message: result,
        }
    client.emit('exception', message)
  }
}
